<template>
    <img class="login-wxgzh" :src="loginImg"/>
</template>

<script>
import loginUrl from "@/views/login/img/loading.gif";
import LoginApi from "@/common/api/login.js";
import {TokenUtil} from '@lowcode/jpaas-base-lib'
import { mapActions } from 'pinia'
import userUser from "../../stores/userUser";
export default {
    name: "PlatformLogin",
    data(){
        return{
            loginImg:loginUrl,
            tenantId: null,
            code: null,
            firstLoad: true,
            redirectUrl:'',
            loginType:2,//登陆类型：1微信公众号，2企业微信，3钉钉，4飞书，5钉钉扫码登陆
        }
    },
    created(){
        this.initData(this.$route.query);
    },
    methods:{
        ...mapActions(userUser,['setToken']),
        initData(item) {
            if (item) {
                this.code = item.code;
                this.tenantId = item.tenantId;
            }
            if (!this.tenantId) {
                this.tenantId = '1';
            }
            //拼接redirectUrl
            this.setLoginUrl();
            //第三方平台回调
            if (this.code) {
                this.platformAutoLogin();
                return;
            }
            //如果是第一次加载且有租户ID，则也尝试一次自动登录（防止之前绑定过但缓存被清除了）
            if (this.firstLoad && this.tenantId) {
                this.firstLoad = false;
                this.getPlatformAuthUrl();
                return;
            }
        },
        //企业微信登录--获取跳转第三方授权地址
        getPlatformAuthUrl() {
            let self = this;
            //获取全路径
            let url = location.href;
            if(url.indexOf("tenantId")==-1){
                url = url +"?tenantId="+this.tenantId;
            }
            let params = {
                tenantId: self.tenantId,
                redirectUrl:this.redirectUrl,
                loginType: this.loginType,
                state: '',
            }
            TokenUtil.removeToken();
            LoginApi.getAuthUrl(params).then(res => {
                //返回成功，则跳转到URL
                if (res.success && res.data) {
                    window.location.href = res.data;
                    return;
                }
            })
        },
        setLoginUrl(){
            let path = this.$route.path;
            let loginUrl = location.href.split(path)[0] +"/PlatformLogin";
            if(this.tenantId){
                loginUrl += "?tenantId=" + this.tenantId;
            }
            this.redirectUrl =  loginUrl;
        },
        //自动登陆
        platformAutoLogin() {
            //根据第三方授权返回的code,进行尝试登陆
            let params = {
                tenantId: this.tenantId,
                code: this.code,
                loginType: this.loginType
            }
            let self_ = this;
            //自动登陆
            LoginApi.platformAutoLogin(params).then(res => {
                //返回成功，则跳转到URL
                if (res.success) {
                    let token = res.data.access_token;
                    if(token){
                        TokenUtil.setToken(token);
                        self_.setToken(token);
                        self_.$router.push({name: 'appHome'}, () => {
                        });
                    }
                } else if (res.code == 10001) {
                    //失败
                    this.$message.warning('获取token失败,请联系管理员同步账号');
                }
            })
        },
    }
}
</script>

<style scoped>
.login-wxgzh{
    width: 100%;
    height: 100%;
}
</style>
